package com.example.administrator.appstpremvpdemo.view.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.NestedScrollView;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.appstpremvpdemo.R;
import com.example.administrator.appstpremvpdemo.common.Constances;
import com.example.administrator.appstpremvpdemo.contract.HomefragentContract;
import com.example.administrator.appstpremvpdemo.helper.StatusLayoutHelper;
import com.example.administrator.appstpremvpdemo.presenter.HomefragentPresenter;
import com.example.administrator.appstpremvpdemo.wegit.CommonAdapter;
import com.example.administrator.appstpremvpdemo.wegit.PageStateLayout;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.lsh.XXRecyclerview.PullRefreshRecycleView;
import com.lsh.XXRecyclerview.XXRecycleView;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by Administrator on 2017/12/7.
 */
public class Homefragent extends SearchHeadFragment implements HomefragentContract.View<String>, XRecyclerView.LoadingListener {


    private StatusLayoutHelper mStatusLayoutHelper;
    private PageStateLayout mPageStatusLayout;
    private HomefragentContract.Presenter mHomePresenter;
    private XRecyclerView mRecyclerView;
    private CommonAdapter mAdapter;
    private NestedScrollView mScrollview;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View mContentview = inflater.inflate(R.layout.homefragent, container, false);
        Log.w("Homefragent", "onCreateView");
        mPageStatusLayout = ((PageStateLayout) mContentview.findViewById(R.id.statuslayout));
        initThisView(mContentview);
        return mContentview;
    }

    private void initThisView(View view) {
        setSearchHeadView(view.findViewById(R.id.search));
        mRecyclerView = ((XRecyclerView) view.findViewById(R.id.re));
        mScrollview = ((NestedScrollView) view.findViewById(R.id.scrollView));
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        mRecyclerView.setLoadingMoreEnabled(true);
        mRecyclerView.setPullRefreshEnabled(true);
        TextView textView = new TextView(getContext());
        textView.setText("我是头");
        //mRecyclerView.addHeaderView(textView);

        mAdapter = new CommonAdapter(null);
        mRecyclerView.setAdapter(mAdapter);
        mAdapter.setItemViewType(Constances.ITEM_TYPE_HOME_LIST);
        //设置presenter，拿数据
        mHomePresenter = new HomefragentPresenter(this, "url");
        mHomePresenter.start();
        mRecyclerView.setLoadingListener(this);

//        mRecyclerView.setOnTouchListener(new View.OnTouchListener() {
//            @Override
//            public boolean onTouch(View v, MotionEvent event) {
//
//                switch (event.getAction()) {
//                case MotionEvent.ACTION_DOWN:
//                    mRecyclerView.requestDisallowInterceptTouchEvent(true);
//                    break;
//                case MotionEvent.ACTION_MOVE:
//
//                    break;
//                case MotionEvent.ACTION_UP:
//
//                    break;
//                }
//                return false;
//            }
//        });
        //mRecyclerView.setNestedScrollingEnabled(false);

        mScrollview.setOnScrollChangeListener(new View.OnScrollChangeListener() {
            @Override
            public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
                Log.w("Homefragent", "scrollX:" + scrollX + "   scrollY:" + scrollY + "----oldScrollX:" + oldScrollX + "   oldScrollY:" + oldScrollY);
                if (scrollY >= 525) {
                    mRecyclerView.setOnTouchListener(new View.OnTouchListener() {
                        @Override
                        public boolean onTouch(View v, MotionEvent event) {
                            switch (event.getAction()) {
                            case MotionEvent.ACTION_DOWN:
                                Log.w("Homefragent", "ACTION_DOWN");
                                //mRecyclerView.getParent().getParent().requestDisallowInterceptTouchEvent(true);
                                break;
                            case MotionEvent.ACTION_MOVE:
                                Log.w("Homefragent", "ACTION_MOVE");
                                //mRecyclerView.getParent().requestDisallowInterceptTouchEvent(false);
                                break;
                            case MotionEvent.ACTION_UP:
                                Log.w("Homefragent", "ACTION_UP");
                                //mRecyclerView.getParent().requestDisallowInterceptTouchEvent(true);
                                break;
                            }
                            return true;
                        }
                    });
                } else if (scrollY < 525) {

                    mRecyclerView.setOnTouchListener(new View.OnTouchListener() {
                        @Override
                        public boolean onTouch(View v, MotionEvent event) {
                            int downy = 0;
                            switch (event.getAction()) {

                            case MotionEvent.ACTION_DOWN:
                                downy = ((int) event.getRawY());
                                Log.w("Homefragent", "downy:" + downy);
                                Log.w("Homefragent", "ACTION_DOWN");
                                //mRecyclerView.getParent().getParent().requestDisallowInterceptTouchEvent(false);
                                break;
                            case MotionEvent.ACTION_MOVE:
                                int moveY = ((int) event.getRawY());
                                Log.w("Homefragent", "ACTION_MOVE");
                                int temp = moveY - downy;
                                Log.w("Homefragent", "temp:" + temp);
                                if (temp > 0) {
                                    Log.w("Homefragent", "上滑");
                                } else {
                                    Log.w("Homefragent", "下滑");
                                }
                                break;
                            case MotionEvent.ACTION_UP:
                                Log.w("Homefragent", "ACTION_UP");
                                break;
                            }
                            return false;
                        }
                    });
                }
            }
        });
    }

    @Override
    public void showDataLoading() {

    }

    @Override
    public void onLoadSuccess(List<String> list) {
        //TODO 获取到数据
        if (mAdapter != null) {
            mAdapter.addAll(list);
        }
    }

    @Override
    public void showDataLoaderror() {

    }

    @Override
    public void showNetError() {

    }

    @Override
    public void setPresenter(HomefragentContract.Presenter presenter) {
        this.mHomePresenter = presenter;
    }

    @Override
    public void onRefresh() {
        mRecyclerView.postDelayed(new Runnable() {
            @Override
            public void run() {
                mAdapter.clear();
                mHomePresenter.start();
                mRecyclerView.refreshComplete();
            }
        }, 2000);
    }

    @Override
    public void onLoadMore() {
        mRecyclerView.postDelayed(new Runnable() {
            @Override
            public void run() {
                mHomePresenter.start();
                mRecyclerView.loadMoreComplete();

                try {
                    mRecyclerView.setLoadingMoreEnabled(false);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }, 2000);
    }
}
